package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.GoodsPayIntegral;
import com.mls.fivedistrict.server.GoodsPayIntegralService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/5/18 8:30
 */
@RestController("backendGoodsPayIntegralController")
@RequestMapping("/api/manager")
public class GoodsPayIntegralController extends BaseController {
    @Autowired
    GoodsPayIntegralService goodsPayIntegralService;

    /**
     * 新增商品支持的积分
     * @param goodsPayIntegral
     * @return
     */
    @PostMapping("/v1/goodsPayIntegral")
    public ServiceResponse addGoodsPayIntegral(@RequestBody GoodsPayIntegral goodsPayIntegral){
        BeanValidator.check(goodsPayIntegral);
        boolean flag = goodsPayIntegralService.insert(goodsPayIntegral);
        if(flag){
            return  ServiceResponse.createSuccessResponse("添加商品支持的积分成功");
        }else{
            return ServiceResponse.createErrorResponse("添加商品支持的积分失败");
        }
    }

    /**
     * 根据id修改商品支持的积分
     * @param goodsPayIntegralId
     * @param goodsPayIntegral
     * @return
     */
    @PutMapping("/v1/goodsPayIntegral/{goodsPayIntegralId}")
    public ServiceResponse editGoodsPayIntegral(@PathVariable Long goodsPayIntegralId,@RequestBody GoodsPayIntegral goodsPayIntegral){
        Optional.ofNullable(goodsPayIntegralId).orElseThrow(() -> new CustomerException("要修改的商品支持积分id不能为空"));
        Optional.ofNullable(goodsPayIntegral).orElseThrow(() -> new CustomerException("要修改的商品支持积分不能为空"));
        goodsPayIntegral.setId(goodsPayIntegralId);
        boolean flag = goodsPayIntegralService.updateById(goodsPayIntegral);
        if(flag){
            return  ServiceResponse.createSuccessResponse("重新配置商品支持的积分成功");
        }else{
            return ServiceResponse.createErrorResponse("重新配置商品支持的积分失败");
        }
    }

    /**
     * 根据id删除商品支持的积分
     * @param goodsPayIntegralId
     * @return
     */
    @DeleteMapping(("/v1/goodsPayIntegral/{goodsPayIntegralId}"))
    public ServiceResponse deleteGoodsPayIntegral(@PathVariable Long goodsPayIntegralId){
        Optional.ofNullable(goodsPayIntegralId).orElseThrow(() -> new CustomerException("要删除的商品支持积分id不能为空"));
        GoodsPayIntegral goodsPayIntegral = new GoodsPayIntegral();
        goodsPayIntegral.setIsDelete(true);
        goodsPayIntegral.setId(goodsPayIntegralId);
        boolean flag = goodsPayIntegralService.updateById(goodsPayIntegral);
        if(flag){
            return ServiceResponse.createSuccessResponse("删除商品支持的积分成功");
        }else{
            return  ServiceResponse.createErrorResponse("删除商品支持的积分失败");
        }
    }

    /**
     * 查看商品支持的积分
     * @return
     */
    @GetMapping("/v1/goodsPayIntegrals")
    public ServiceResponse getGoodsPayIntegralList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = goodsPayIntegralService.selectList(pageFilter.getWrapper());
        PageInfo<GoodsPayIntegral> goodsPayIntegralPageInfo = new PageInfo<GoodsPayIntegral>(selectList, 5);
        return ServiceResponse.createSuccessResponse(goodsPayIntegralPageInfo);
    }

    /**
     * 根据id查询商品支持积分
     * @param goodsPayIntegralId
     * @return
     */
    @GetMapping("/v1/goodsPayIntegral/{goodsPayIntegralId}")
    public ServiceResponse getGoodsPayIntegral(@PathVariable Long goodsPayIntegralId){
        Optional.ofNullable(goodsPayIntegralId).orElseThrow(() -> new CustomerException("要查询的商品支持积分id不能为空"));
        GoodsPayIntegral goodsPayIntegral = goodsPayIntegralService.selectById(goodsPayIntegralId);
        if(null!=goodsPayIntegral){
            return ServiceResponse.createSuccessResponse(goodsPayIntegral);
        }else{
            return ServiceResponse.createErrorResponse("该种商品支持积分不存在");
        }
    }

}
